Skip to content

Expose default_color_mode in rx.Config#6716

Open
masenf wants to merge 9 commits into
mainfrom
masenf/default-color-mode
Open

Expose default_color_mode in rx.Config#6716
masenf wants to merge 9 commits into
mainfrom
masenf/default-color-mode

Conversation

@masenf

@masenf masenf commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

@masenf masenf requested a review from a team as a code owner July 7, 2026 22:29
@greptile-apps

greptile-apps Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR exposes default_color_mode in rx.Config (accepting "system", "light", or "dark", also settable via REFLEX_DEFAULT_COLOR_MODE), allowing apps to control the initial color mode without requiring Radix themes. The value is validated at config init time and threaded into both the compiled JS context and the pre-hydration preload script.

  • Config field + validation: default_color_mode: LiteralColorMode = "system" added to BaseConfig; Config.__init__ validates it after env-var resolution using LiteralColorMode.__args__.
  • Color mode constants refactored: SYSTEM_COLOR_MODE, LIGHT_COLOR_MODE, DARK_COLOR_MODE, and LiteralColorMode moved from style.py into reflex_base.constants.base and re-exported from style.py to preserve the public API.
  • JS context/provider refactored: ColorModeContext.Provider (with toggleColorMode/setColorMode) moved from RadixThemesColorModeProvider into the always-present ThemeProvider, so color-mode context is available without Radix; the preload script now uses the resolved default_color_mode as its localStorage fallback to prevent flash of wrong theme.

Confidence Score: 5/5

The change is well-scoped — a new config field with validation, a constants refactor, and a JS provider restructuring, all covered by targeted tests.

Config validation runs after env-var resolution so invalid values (including those from REFLEX_DEFAULT_COLOR_MODE) are caught early. The JS refactoring correctly moves context provision to ThemeProvider and the preload script default is consistent with the compiled context value. Tests cover all three valid modes, the precedence logic, env-var override, and document-root compilation.

No files require special attention.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/config.py Adds default_color_mode: LiteralColorMode = "system" field to BaseConfig and validates it in Config.__init__ against constants.LiteralColorMode.__args__ after env-var resolution.
packages/reflex-base/src/reflex_base/constants/base.py Extracts SYSTEM_COLOR_MODE, LIGHT_COLOR_MODE, DARK_COLOR_MODE string constants and LiteralColorMode type alias from style.py into the shared constants module.
reflex/compiler/compiler.py Adds _resolve_default_color_mode helper that chooses between an explicit rx.theme(appearance=...) and Config.default_color_mode; threads the resolved value into both compile_contexts and compile_document_root.
reflex/utils/misc.py Converts preload_color_theme to accept a default_color_mode argument, injecting it into the inline script via an f-string so the pre-hydration fallback matches the compiled context.
packages/reflex-base/src/reflex_base/.templates/web/utils/react-theme.js Moves ColorModeContext.Provider (with toggleColorMode / setColorMode) from RadixThemesColorModeProvider into ThemeProvider, so color-mode context is available without Radix.
packages/reflex-base/src/reflex_base/.templates/web/components/reflex/radix_themes_color_mode_provider.js Stripped down to a thin Radix-specific CSS effect; context providing is now handled by ThemeProvider.
reflex/compiler/utils.py Passes default_color_mode through to preload_color_theme when creating the document root.
tests/units/compiler/test_compiler.py Adds comprehensive parametrized tests for _resolve_default_color_mode, compile_contexts, preload_color_theme, and compile_document_root; uses an isolated config mock to avoid singleton leakage.
tests/units/test_config.py Adds tests for the default_color_mode default value and env-var override via REFLEX_DEFAULT_COLOR_MODE.
packages/reflex-base/src/reflex_base/style.py Removes locally-defined color mode constants/type and re-imports them from reflex_base.constants.base (re-exporting with as aliases to preserve public API).

Reviews (8): Last reviewed commit: "validate Config.default_color_mode" | Re-trigger Greptile

@codspeed-hq

codspeed-hq Bot commented Jul 7, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing masenf/default-color-mode (0f55fea) with main (9a5c4d3)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Comment thread tests/units/compiler/test_compiler.py Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f5a912f962

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread reflex/compiler/compiler.py Outdated
masenf added 8 commits July 7, 2026 15:41
Decouple the actual toggle / set color mode functionality from radix so it can
be used without including the RadixThemesColorModeProvider.

Simplify RadixThemesColorModeProvider to only be responsible for flipping the
.radix-themes root classes.
use setColorMode instead of setTheme for improved validation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants